﻿Imports MySql.Data.MySqlClient
Imports System.Data.SqlClient
Imports System.Data
Imports System.Text


Public Class Importer
    Dim macnx As New PPE3.ClassMySqlConnect("853339_depanneur", "071064")

    Private Sub Importer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MdiParent = FrmStart
        Me.Left = 0
        Me.Top = 0


        rafraichir()
        BtnImporter.Enabled = False


    End Sub


    Private Sub BtnParcourir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnParcourir.Click

        OpenFileDialog1.Filter = "CSV|*.csv|TXT|*.txt"

        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            TxtFichier.Text = OpenFileDialog1.FileName
        End If
        If TxtFichier.Text = "" Then
            BtnImporter.Enabled = False
        Else
            BtnImporter.Enabled = True
        End If


    End Sub

    Private Sub rafraichir()
        macnx.Se_Connecter_MySqlServer()
        dgmat.DataSource = Nothing
        macnx.viderVue("Toutmateriel")
        dgmat.DataSource = macnx.Executer_Requete_Select("select idmaterielNeuf,ReferenceMaterielNeuf,NomMaterielNeuf,CategorieNeuf,prixMaterielNeuf from materielneuf ", "Toutmateriel")
        macnx.Se_Deconnecter_MySqlServer()

        If dgmat.RowCount - 1 Then
            BtnVider.Enabled = True
            BtnParcourir.Enabled = False
        Else
            BtnVider.Enabled = False
            BtnParcourir.Enabled = True

        End If

    End Sub

    Private Sub BtnImporter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnImporter.Click


        Dim a As String
        Dim NbLignes As Integer
        Dim cmd As MySqlCommand = New MySqlCommand

        a = TxtFichier.Text
        TxtFichier.Text = TxtFichier.Text.Replace("\", "\\")
        TxtFichier.Text = TxtFichier.Text.Replace(":\\", ":\")
        'cmd.Connection = connection.Cnx

        macnx.Se_Connecter_MySqlServer()
        macnx.Executer_Requete_Action("LOAD DATA INFILE '" & TxtFichier.Text & "' REPLACE INTO TABLE materielneuf FIELDS TERMINATED BY ';' ESCAPED BY '\\' LINES TERMINATED BY '\r\n'IGNORE 1 LINES;")
        macnx.Se_Deconnecter_MySqlServer()

        Try
            NbLignes = cmd.ExecuteNonQuery()
            MessageBox.Show(NbLignes & " lignes viens d'étres insérées.", "IMPORTATION")
            rafraichir()
            BtnImporter.Enabled = False
            TxtFichier.Text = ""
        Catch exms As MySqlException
            MessageBox.Show(exms.Message)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try


    End Sub

    Private Sub BtnVider_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnVider.Click
        macnx.Se_Connecter_MySqlServer()
        macnx.viderVue("Toutmateriel")
        macnx.Executer_Requete_Action("Delete From materielneuf")
        macnx.Se_Deconnecter_MySqlServer()
        
        rafraichir()

    End Sub
End Class
